﻿using CommandProcessor.Command;
using Data.Infrastructure;
using Data.Models;
using Data.Repositories;
using Domain.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Handlers
{
    public class CreateOrUpdateProductHandler : ICommandHandler<CreateOrUpdateProductCommand>
    {
        private readonly IProductRepository ProductRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateProductHandler(IProductRepository ProductRepository, IUnitOfWork unitOfWork)
        {
            this.ProductRepository = ProductRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateProductCommand command)
        {
            var Product = new Product
            {
                ID = command.ID,
                Code = command.Code,
                Attachment=command.Attachment,
                CategoryID=command.CategoryID,
                Description=command.Description,
                Name=command.Name,
                ProductTypeID=command.ProductTypeID,
                ParentID=command.ParentID,
                Level=command.Level,
                ImageDesign=command.ImageDesign,
            };
            if (Product.ID == 0)
                ProductRepository.Add(Product);
            else
                ProductRepository.Update(Product);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateProductCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateProductCommand> commands)
        {
            throw new NotImplementedException();
        }
    }
}
